package com.blockmeta.bbs.immodule.adapter;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.blockmeta.bbs.baselibrary.base.adapter.BaseAdapter;
import com.blockmeta.bbs.baselibrary.utils.LoadPicUtil;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.bean.beaninterface.ICircleEntry;
import com.blockmeta.bbs.businesslibrary.widget.CircleGradeAndKind;
import com.blockmeta.bbs.immodule.widget.CircleItemDecoration;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * @author AirThor
 * @date 2019/3/7.
 */
public class IMCircleShareAdapter extends BaseAdapter<ICircleEntry, BaseViewHolder> {

    private ICircleEntry mSelectedEntry = null;

    public IMCircleShareAdapter(int layoutResId, @Nullable List<ICircleEntry> data) {
        super(layoutResId, data);
    }

    public ICircleEntry getCheckedItems() {
        return mSelectedEntry;
    }

    @Override
    public void bindToRecyclerView(RecyclerView recyclerView) {
        super.bindToRecyclerView(recyclerView);
        recyclerView.addItemDecoration(new CircleItemDecoration(this));
    }

    @Override
    protected void convert(final BaseViewHolder helper, final ICircleEntry item) {
        TextView tvJoinLimit = helper.getView(R.id.tv_join_limit);
        Button btnJoin = helper.getView(R.id.btn_join);
        tvJoinLimit.setVisibility(View.GONE);
        btnJoin.setVisibility(View.GONE);
        ImageView ivLogo = helper.getView(R.id.iv_avatar);
        LoadPicUtil.loadForIm(ivLogo,item.getCircleLogo());

        helper.setText(R.id.tv_circle_name,item.getCircleName());

        helper.setText(R.id.tv_circle_tag,item.getCircleSign());

        helper.setText(R.id.tv_circle_size,item.getCircleSize() + "人");

        helper.setText(R.id.tv_join_limit,item.getCircleJoinLimit());
        helper.setGone(R.id.tv_circle_ismy, item.getCreateById() == AccountHelper.getUidInt());

        CircleGradeAndKind circleGradeAndKind = helper.getView(R.id.cgk_info);
        circleGradeAndKind.setText(item.getCircleGrade(),item.getCircleKind());

        helper.setGone(R.id.iv_im_sign,item.isIdentification());

        final ToggleButton toggleButton = helper.getView(R.id.tg_selector);
        toggleButton.setChecked(item==mSelectedEntry);
        View itemClick = helper.getView(R.id.item_click);
        itemClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = !toggleButton.isChecked();
                toggleButton.setChecked(isChecked);
                if (isChecked){
                    mSelectedEntry = item;
                }else {
                    mSelectedEntry = null;
                }
                notifyDataSetChanged();
            }
        });
    }
}
